package org.linlinjava.litemall.brand.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.brand.vo.CategoryVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.service.LitemallCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand/category")
@Validated
public class BrandCategoryController {
    private final Log logger = LogFactory.getLog(BrandCategoryController.class);

    @Autowired
    private LitemallCategoryService categoryService;

    /**
     * 获取所有的 L1 级别..带L2 子类.  05.15 增加 L级别分类
     * @return
     */
    @GetMapping("/list")
    public Object list() {
        List<CategoryVo> categoryVoList = new ArrayList<>();
        List<LitemallCategory> categoryList = categoryService.queryByPid(0);
        for (LitemallCategory category : categoryList) {
            CategoryVo categoryVO = new CategoryVo();
            BeanUtils.copyProperties(category,categoryVO);
            List<CategoryVo> children = new ArrayList<>();
            List<LitemallCategory> subCategoryList = categoryService.queryByPid(category.getId());
            for (LitemallCategory subCategory : subCategoryList) {
                CategoryVo subCategoryVo = new CategoryVo();
                BeanUtils.copyProperties(subCategory,subCategoryVo);
                children.add(subCategoryVo);
            }
            categoryVO.setChildren(children);
            categoryVoList.add(categoryVO);
        }
        return ResponseUtil.okList(categoryVoList);
    }

    private static final String  CATEGORY_L1= "L1";
    private static final String  CATEGORY_L2= "L2";
    private static final String  CATEGORY_L3= "L3";

    /**
     * 验证 分类category 信息  1名字不空 2 类别不空  3 必须包含3个级别  4 L2和L3必须有PID
     * @param category
     * @return
     */
    private Object validate(LitemallCategory category) {
        String name = category.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        //类别不能为空
        String level = category.getLevel();
        if (StringUtils.isEmpty(level)) {
            return ResponseUtil.badArgument();
        }
        //类别必须包含 L1  L2 L3 级别
        if(!CATEGORY_L1.equals(level) && !CATEGORY_L2.equals(level) && !CATEGORY_L3.equals(level) ){
            return ResponseUtil.badArgumentValue();
        }
        //必须 L2 有父类的PID
        Integer pid = category.getPid();
        if (CATEGORY_L2.equals(level) && (pid == null)) {
            return ResponseUtil.badArgument();
        }
        //必须 L3 有父类的PID--> L2
        if( CATEGORY_L3.equals(level) && (pid == null)){
            return ResponseUtil.badArgument();
        }
        return null;
    }

    /**
     * 新增  检测类别
     * @param category
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody LitemallCategory category) {
        Object error = validate(category);
        if (error != null) {
            return error;
        }
        categoryService.add(category);
        return ResponseUtil.ok(category);
    }

    /**
     * 主键 查找  分类类别
     * @param id
     * @return
     */
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        LitemallCategory category = categoryService.findById(id);
        return ResponseUtil.ok(category);
    }

    /**
     * 更新类别信息
     * @param category
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody LitemallCategory category) {
        Object error = validate(category);
        if (error != null) {
            return error;
        }
        //更新信息返回0  返回更新错误
        if (categoryService.updateById(category) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok();
    }

    /**
     * 删除类别
     * @param category
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallCategory category) {
        Integer id = category.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        categoryService.deleteById(id);
        return ResponseUtil.ok();
    }

    @GetMapping("/l1")
    public Object catL1() {
        // 所有一级分类目录
        List<LitemallCategory> l1CatList = categoryService.queryL1();
        List<Map<String, Object>> data = new ArrayList<>(l1CatList.size());
        for (LitemallCategory category : l1CatList) {
            Map<String, Object> d = new HashMap<>(2);
            d.put("value", category.getId());
            d.put("label", category.getName());
            data.add(d);
        }
        return ResponseUtil.okList(data);
    }


}
